/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Courseresourcesauditlog;
import me.zhengjie.modules.system.domain.Courseresourcescatalog;
import me.zhengjie.modules.system.service.CourseresourcesauditlogService;
import me.zhengjie.modules.system.domain.vo.CourseresourcesauditlogQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.HashMap;
import java.util.List;

import me.zhengjie.modules.system.service.CourseresourcescatalogService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author shitou
* @date 2024-12-12
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "课程资源审核日志管理")
@RequestMapping("/api/courseresourcesauditlog")
public class CourseresourcesauditlogController {

    private final CourseresourcesauditlogService courseresourcesauditlogService;

    private final CourseresourcescatalogService courseresourcescatalogService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('courseresourcesauditlog:list')")
    public void exportCourseresourcesauditlog(HttpServletResponse response, CourseresourcesauditlogQueryCriteria criteria) throws IOException {
        courseresourcesauditlogService.download(courseresourcesauditlogService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询课程资源审核日志")
    @ApiOperation("查询课程资源审核日志")
//    @PreAuthorize("@el.check('courseresourcesauditlog:list')")
    public ResponseEntity<PageResult<Courseresourcesauditlog>> queryCourseresourcesauditlog(CourseresourcesauditlogQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(courseresourcesauditlogService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
//    @Log("新增课程资源审核日志")
    @ApiOperation("新增课程资源审核日志")
//    @PreAuthorize("@el.check('courseresourcesauditlog:add')")
    public ResponseEntity<Object> createCourseresourcesauditlog(@Validated @RequestBody Courseresourcesauditlog resources){
        if(resources.getState()==0 && resources.getCourseResourcesCatalogId()!=null && resources!=null){

            List<Courseresourcescatalog> courseresourcesauditlogList = courseresourcescatalogService.queryOthenMsg(resources.getCourseResourcesCatalogId());

            if(courseresourcesauditlogList.size()!=0){
                Map<String,Object> map = new HashMap<>();
                map.put("msg","该资源已审核完毕，请刷新页面");
                map.put("code",1);
                return new ResponseEntity<>(map, HttpStatus.valueOf(200));
            }
        }

//        if(resources.getState()==3){
//
//            List<Courseresourcescatalog> courseresourcesauditlogList = courseresourcescatalogService.queryOthenMsg2(resources.getCourseResourcesCatalogId());
//            if(courseresourcesauditlogList.size()!=0){
//                Map<String,Object> map = new HashMap<>();
//                map.put("msg","该目录已撤回审核，请刷新页面");
//                map.put("code",1);
//                return new ResponseEntity<>(map, HttpStatus.valueOf(200));
//            }
//        }

        if(resources.getState()==2){

            List<Courseresourcescatalog> courseresourcesauditlogList = courseresourcescatalogService.queryOthenMsg3(resources.getCourseResourcesCatalogId());
            if(courseresourcesauditlogList.size()!=0){
                Map<String,Object> map = new HashMap<>();
                map.put("msg","该目录已撤回审核，请刷新页面");
                map.put("code",1);
                return new ResponseEntity<>(map, HttpStatus.valueOf(200));
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("msg","suceess");
        map.put("code",0);
        courseresourcesauditlogService.create(resources);
        return new ResponseEntity<>(map, HttpStatus.valueOf(200));
    }

    @PutMapping
    @Log("修改课程资源审核日志")
    @ApiOperation("修改课程资源审核日志")
//    @PreAuthorize("@el.check('courseresourcesauditlog:edit')")
    public ResponseEntity<Object> updateCourseresourcesauditlog(@Validated @RequestBody Courseresourcesauditlog resources){
        courseresourcesauditlogService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除课程资源审核日志")
    @ApiOperation("删除课程资源审核日志")
//    @PreAuthorize("@el.check('courseresourcesauditlog:del')")
    public ResponseEntity<Object> deleteCourseresourcesauditlog(@RequestBody List<Integer> ids) {
        courseresourcesauditlogService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}